/*
  1获取用户收货地址
    1.绑定点击事件
    2.调用小程序内置api，获取用户收货地址
*/
import {getSetting,chooseAddress,openSetting,showModal,showToast} from '../../utils/asyncWx.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Address:{},
    cart:[],
    checkout:false
  },
  // 点击收货按钮
 async handleChooseAddress(){
   try {
    const res1 = await getSetting()
    const scopeAddress = res1.authSetting["scope.address"]
    if(scopeAddress===false){
      await openSetting()
    }
    const Address = await chooseAddress()
    Address.all = Address.provinceName+Address.cityName+Address.countyName+Address.detailInfo
    wx.setStorageSync('Address', Address)
    console.log(res2);
   } catch (error) {
     console.log(error);
     
   }
    
    /*
    // wx.getSetting({
    //  success:(result)=>{
    //    console.log(result);
       
    //    const authSrttingAddress = result.authSetting["scope.address"]
    //    if(authSrttingAddress===true||authSrttingAddress===undefined)
    //    {
    //       wx.chooseAddress({
    //         success: (result1) => {
    //           console.log(result1);
    //         },
    //       })
    //    }
    //    else{
    //     wx.openSetting({
    //       success:(result2)=>{
    //         wx.chooseAddress({
    //           success: (result3) => {
    //             console.log(result3);
    //           },
    //         })
    //       }
    //     })
    //    }
    //  }
    // })
    */
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let Address = wx.getStorageSync('Address')
    let cart = wx.getStorageSync('cart')||[]
    // let checkout = cart.length?cart.every(v=>v.checked):false
    this.setData({
      Address
    })
    this.bindClick(cart)
  },
  bindItemClick(e){
    // 获取勾选商品id
    var goods_id = e.currentTarget.dataset.id
    // 找到商品
    let {cart} = this.data
   let index= cart.findIndex(v=>v.goods_id===goods_id)
    cart[index].checked = !cart[index].checked
    this.bindClick(cart)
  },
  // 全选
  bindAllClick(){
    // 获取 data 中数据
    let {cart,checkout} = this.data
    // 设置全选
    checkout = !checkout
    cart.forEach(v=>v.checked=checkout)
    this.bindClick(cart)
  },
  // 封装勾选方法
  bindClick(cart){
     // 默认选中
     let checkout = true
     let cartPrice=0
     let cartNum=0
     cart.forEach(v=>{
      if(v.checked){
       cartPrice+=v.num*v.goods_price
       cartNum+=v.num
      }
      else{
       checkout=false
      }
     })
     checkout=cart.length!=0?checkout:false
     this.setData({
       cart,
       checkout,
       cartPrice,
       cartNum
     })
     wx.setStorageSync('cart', cart)
  },
  // 商品数量增减
 async bindItemNumEdit(e){
    // 获取商品id 和 操作
    let {opertion,id}=e.currentTarget.dataset
    let {cart} = this.data
    let index = cart.findIndex(v=>v.goods_id===id)
   
   if(cart[index].num===1&&opertion===-1){
    const res = await showModal({content:"是否删除该商品"})
    if(res.confirm){
      cart.splice(index,1)
      this.bindClick(cart)
    }
    return
   }
   else{
    cart[index].num+=opertion
    this.bindClick(cart)
   }
   
    

    
  },
  // 商品结算
async Settlement(){
    const {Address,cartNum} = this.data
   if(!Address.userName){
    await showToast({title:'收货地址地址不能为空'})
    return
   }
   if(cartNum===0){
     await showToast({title:'购物车还没有商品'})
    return
   }
   wx.navigateTo({
     url: '/pages/pay/index',
   })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})